<?php

namespace Admin\Controller;

use Think\Controller;

class AttrkController extends AdminController
{

    public function index()
    {
        $name = I('name');
        if (is_numeric($name)) {
            $map['id|name'] = array(intval($name), array('like', '%' . $name . '%'), '_multi' => true);
        } else {
            $map['name'] = array('like', '%' . (string) $name . '%');
        }

        $list = $this->lists('attrk', $map);
        $aa   = M('Goods')->getField('id,name');
        int_to_string($list);
        $this->assign('_list', $list);
        $this->assign('aa', $aa);
        $this->meta_title = '商品信息';
        $this->display();
    }
    public function add()
    {
        if (IS_GET) {
            $a   = I('get.id');
            $res = M('attrk')->where(['id' => $a])->select();
            $this->assign('res', $res);
            $this->display();
        } else {
            $data = I('post.');
            $res  = M('attrv')->add($data);
            if ($res) {
                $this->success('成功');
            } else {
                $this->error('失败');
            }
        }
    }
    public function update()
    {
        $attrk = M('attrk');

        if (IS_GET) {
            $res = $attrk->find(I('get.id'));
            $this->assign('res', $res);
            $this->display();
        } else {
        	$data = I('post.');
            if ($data) {
                $attrk->where(['id' => I('post.id')])->save($data);
                $this->success('修改成功', U('index'));

            } else {
                $this->error($attrk->getError());
            }

        }

    }
    public function changeStatus($method = null)
    {
        $id = array_unique((array) I('id', 0));
        $id = is_array($id) ? implode(',', $id) : $id;
        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }
        $map['id'] = array('in', $id);
        switch (strtolower($method)) {
            case 'hide':
                $this->forbid('Attrk', $map);
                break;
            case 'show':
                $this->resume('Attrk', $map);
                break;
            case 'del':
                D('Attrk')->where($map)->delete();
                $this->success('删除成功');
                break;
            default:
                $this->error('参数非法');
        }
    }
}
